package com.cirkana.viewer.route;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.cirkana.ImageUtil;
import com.cirkana.PassData;
import com.cirkana.ViewerActivity;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.viewer.R;
import com.cirkana.viewer.data.route.RouteDetail;

public class RouteDetailActivity extends ViewerActivity {

	private EditText titleEditText;
	private EditText descriptionEditText;
	private ImageView previewImageView;
	private Button selectButton;

	private RouteDetailPassData passData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.routedetail);

		this.titleEditText = (EditText) findViewById(R.id.routedetail_title);
		this.descriptionEditText = (EditText) findViewById(R.id.routedetail_description);
		this.previewImageView = (ImageView) findViewById(R.id.routedetail_previewimage);
		this.selectButton = (Button) findViewById(R.id.routedetail_select);

		this.passData = (RouteDetailPassData) getRequest(RouteDetailPassData.class);

		setRouteDetailInfo(this.passData.getRouteDetail());

		this.selectButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					setResponse(RESULT_OK, passData);
				} catch (BinarySerializationException e) {
					openMessageBox("Error", e.getMessage());
				}
				finish();
			}
		});

	}

	private void setRouteDetailInfo(RouteDetail routeDetail) {

		this.titleEditText.setText(routeDetail.getTitle());
		this.descriptionEditText.setText(routeDetail.getDescription());
		try {
			this.previewImageView.setImageBitmap(ImageUtil.getImageBitmap(routeDetail.getPreviewImage()));
		} catch (Exception e) {
			showErrorMessage(e);
		}
	}

	public static class RouteDetailPassData extends PassData {

		private RouteDetail routeDetail;

		public RouteDetail getRouteDetail() {
			return routeDetail;
		}

		public void setRouteDetail(RouteDetail routeDetail) {
			this.routeDetail = routeDetail;
		}

		public RouteDetailPassData() {
			super();
		}

		public RouteDetailPassData(RouteDetail routeDetail) {
			this.routeDetail = routeDetail;
		}

		@Override
		public void serialize(DataOutputStream out) throws BinarySerializationException {
			BinarySerializationUtil.serializeSerializable(this.routeDetail, out);
		}

		@Override
		public void deserialize(DataInputStream in) throws BinarySerializationException {
			this.routeDetail = (RouteDetail) BinarySerializationUtil.deserializeSerializable(RouteDetail.class, in);
		}

		@Override
		public String toString() {
			StringBuilder s = new StringBuilder("{");

			s.append("routeDetail=");
			if (this.routeDetail == null)
				s.append("(null)");
			else
				s.append(this.routeDetail.toString());
			s.append("}");

			return s.toString();
		}

	}
}
